package com.azunitech.guice;

import java.io.InputStream;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
 
public class SimpleEHCache {
	private static final CacheManager cacheManager;
     
    static {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        InputStream resourceAsStream = contextClassLoader.getResourceAsStream("ehcache.xml");
        cacheManager = CacheManager.create(resourceAsStream);
    }
     
    private Ehcache employeeCache;
     
    public SimpleEHCache() {
        employeeCache = cacheManager.getEhcache("employees");
    }
     
    public void addEmployee(Integer id, Employee employee) {
        // Create an EHCache Element to hold the Employee
        Element element = new Element(id, employee);
         
        // Add the element to the cache
        employeeCache.put(element);
    }
     
    public Employee getEmployee(Integer id) {
        // Retrieve the element that contains the requested Employee
        Element element = employeeCache.get(id);
        if (element != null) {
            return (Employee) element.getObjectValue();
        }
         
        return null;
    }
}